<?php
/**
 *
 * @Author:      yyc
 * @DateTime:    18-7-18 上午10:00
 * @Description: 消息模型
 */

class OANewsNotice extends Eloquent
{
    /**
     * @var string 表名
     */
    protected $table = 'oa_news_notice';
    /**
     * 阅读、忽略、删除状态
     * @var array
     */
    public static $READ_STATUS = [
        'UNREAD'  => 1,
        'READ'    => 2,
        'IGNORE'  => 3,
        'DELETE'  => 4,
    ];

    /**
     * @Author:      yyc
     * @DateTime:    18-7-18 下午14:00
     * @Description: 获取未读消息信息
     * @param $userID
     * @return mixed
     */
    public static function getNewsNoticeInfo($userID)
    {
        $selectData = [
            'oa_news_notice.news_type', 'pc', 'wechat', 'start_user_id', 'show_time', 'start_department_id',
            'oa_news_notice.content', 'oa_news_notice.created_at', 'start_user.real_name as start_user',
            'oa_news_notice.apply_id', 'oa_news_notice.start_department', 'op_user_names', 'user_type',
            'oa_news_notice.start_at', 'op_user_ids', 'op_type', 'oa_news_notice.created_at',
            'oa_news_notice.updated_by', 'oa_news_notice.id', 'oa_news_notice.user_type'
        ];
        $result = OANewsNotice::where('to_user_id', $userID)
            ->select($selectData)
            ->where('oa_news_notice.status', self::$READ_STATUS['UNREAD'])
            ->where('oa_news_notice.show_time', '<', date('Y-m-d H:i:s', time()))
            ->where('oa_news_notice.pc', '=', 1)
            ->join('oa_user as start_user', 'start_user.id', '=', 'oa_news_notice.start_user_id')
            ->orderBy('oa_news_notice.created_at' , 'asc')
            ->get()
            ->toArray();

        return [
            'count' => count($result),
            'infoNews' => self::processingTime($result),
        ];
    }

    /**
     * 转化时间格式  'Y-m-d H:i:s'  ---->   'Y-m-d H:i'
     * @param $result
     * @return array
     * @author yyc
     * @date 2018/8/17 16:22
     */
    public static function processingTime($result)
    {
        if (empty($result)) {
            return [];
        }
        foreach ($result as &$value) {
            $value['created_at'] = date('Y-m-d H:i', strtotime($value['created_at']));
        }

        return $result;
    }

    /**
     * @Author:      zk
     * @DateTime:    18-8-3 下午1:28
     * @Description: 根据申请编号添加申请人的 申请消息   【 给申请人发送提醒通知 】
     * @param int $apply_id  申请id
     * @param int $apply_status 申请状态  3:同意;4:不同意; 【最终 审批完成时 必传、每个节点审批结束时不传】
     * @param int $op_user_id 最后操作人id [整体流程同意/不同意/撤回 时的用户id]
     * @return boolean
     */
    public static function addNewsToApplyPeople($apply_id, $apply_status = '', $op_user_id = '') {
        $result = FALSE;
        $apply_info = OAApply::where('id', $apply_id)->first();
        if(!$apply_info){
            return $result;
        }
        $applyModel = OAApprovalModel::where('id', $apply_info->approval_model_id)->first();
        if (!$applyModel) {
            return $result;
        }

        $news = new OANewsNotice();
        $news->news_type = 1;   //消息类型 1:审批提醒
        //$news->content = $apply_info->approval_type_name.'申请';   //消息内容
        $news->content = $applyModel->name;   //消息内容
        $news->user_type = 1;   //消息的用户接收类型(1:申请人;2:审批人;3:抄送人;4:转批接收人;)
        $news->to_user_id = $apply_info->created_by;    //消息接收人
        $news->pc = $apply_info->pc;
        $news->wechat = $apply_info->wechat;
        $news->email = $apply_info->email;
        $news->status = 1;  //状态 (1:未读;2:已读;3:已忽略;4:已删除;)
        $news->start_user_id = $apply_info->created_by;
        $news->apply_id = $apply_id;
        $news->start_department_id = $apply_info->hrm_department_id;
        $news->start_department = $apply_info->hrm_department;
        $news->show_time = Common::num2DateTime(time());
        $news->start_at = $apply_info->created_at;
        $news->created_at = Common::num2DateTime(time());
        $news->created_by = 0;  //创建人(0:系统;)
        if($apply_status != ''){
            //审批完成 ……   申请状态  3:同意;4:不同意
            if($apply_status == 3){
                $news->op_type = 3;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
            }elseif ($apply_status == 4) {
                $news->op_type = 2;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
            } else {
                return $result;
            }
            //获取最后操作人信息 start ……
            $op_user_ids = '';
            $op_user_names = '';
            if ($op_user_id != ''){
                $op_user_info = OAUser::getUserById($op_user_id);
                if($op_user_info){
                    $op_user_ids = $op_user_info->id;
                    $op_user_names = $op_user_info->real_name;
                }
            }
            $news->op_user_ids = $op_user_ids;
            $news->op_user_names = $op_user_names;
            //获取最后操作人信息 end ……
        } else {
            //审批中
            $approver_info = OAApplyFlow::getApproverByApplyId($apply_id, 1, $apply_info->level);   //获取下级审批人
            if(empty($approver_info)){
                return $result;
            }
            $op_user_ids = '';
            $op_user_names = '';
            foreach ($approver_info as $k => $v) {
                $op_user_ids .= $v['user_id'].',';
                $op_user_names .= $v['real_name'].',';
            }
            $op_user_ids = rtrim($op_user_ids, ',');
            $op_user_names = rtrim($op_user_names, ','); 
            
            $news->op_user_ids = $op_user_ids;
            $news->op_user_names = $op_user_names;
            $news->op_type = 1;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回;
        }
        
        return $news->save();
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-8-6 上午9:59
     * @Description: 根据申请编号添加给抄送人的消息提醒     【 给抄送人发送提醒通知 】
     * @param int $apply_id 申请id
     * @param int $apply_status 申请状态    (3:同意;4:不同意;5:已撤回;) 【最终审批完成时或者申请人撤回时 必传】
     * @param int $op_user_id 最后操作人id [整体流程同意/不同意/撤回 时的用户id]
     * @return boolean
     */
    public static function addNewsToCc($apply_id ,$apply_status = '', $op_user_id = '') {
        $result = FALSE;
        $apply_info = OAApply::where('id', $apply_id)->first();
        if(!$apply_info){
            return $result;
        }
        $applyModel = OAApprovalModel::where('id', $apply_info->approval_model_id)->first();
        if (!$applyModel) {
            return $result;
        }
        //获取本次申请需要抄送的抄送人信息
        $cc_info = OAApplyFlow::getApproverByApplyId($apply_id, 2);
        if(empty($cc_info)){
            return TRUE;
        }
        $insert_data = [];  //待插入数据
        if($apply_status != ''){
            //审批完成 ……   申请状态  3:同意;4:不同意;5:已撤回;
            switch ($apply_status) {
                case 3:
                    $op_type = 3;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
                    break;
                case 4:
                    $op_type = 2;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
                    break;
                case 5:
                    $op_type = 4;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
                    break;
                default:
                    return $result;
                    break;
            }
            //获取最后操作人信息 start ……
            $op_user_ids = '';
            $op_user_names = '';
            if ($op_user_id != ''){
                $op_user_info = OAUser::getUserById($op_user_id);
                if($op_user_info){
                    $op_user_ids = $op_user_info->id;
                    $op_user_names = $op_user_info->real_name;
                }
            }
            //获取最后操作人信息 end ……
            foreach ($cc_info as $k => $v) {
                $temp['news_type'] = 1;   //消息类型 1:审批提醒
                //$temp['content'] = $apply_info->approval_type_name.'申请';   //消息内容
                $temp['content'] = $applyModel->name;   //消息内容
                $temp['user_type'] = 3;   //消息的用户接收类型(1:申请人;2:审批人;3:抄送人;4:转批接收人;)
                $temp['to_user_id'] = $v['user_id'];    //消息接收人
                $temp['pc'] = $apply_info->pc;
                $temp['wechat'] = $apply_info->wechat;
                $temp['email'] = $apply_info->email;
                $temp['status'] = 1;  //状态 (1:未读;2:已读;3:已忽略;4:已删除;)
                $temp['start_user_id'] = $apply_info->created_by;
                $temp['apply_id'] = $apply_id;
                $temp['start_department_id'] = $apply_info->hrm_department_id;
                $temp['start_department'] = $apply_info->hrm_department;
                $temp['show_time'] = Common::num2DateTime(time());
                $temp['start_at'] = $apply_info->created_at;
                $temp['created_at'] = Common::num2DateTime(time());
                $temp['created_by'] = 0;  //创建人(0:系统;)
                $temp['op_user_ids'] = $op_user_ids;
                $temp['op_user_names'] = $op_user_names;
                $temp['op_type'] = $op_type;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回;
                
                $insert_data[] = $temp;
            }
        } else {
            //审批中
            $approver_info = OAApplyFlow::getApproverByApplyId($apply_id, 1, $apply_info->level);   //获取下级审批人
            if(empty($approver_info)){
                return $result;
            }
            $op_user_ids = '';
            $op_user_names = '';
            foreach ($approver_info as $k => $v) {
                $op_user_ids .= $v['user_id'].',';
                $op_user_names .= $v['real_name'].',';
            }
            $op_user_ids = rtrim($op_user_ids, ',');
            $op_user_names = rtrim($op_user_names, ','); 
            
            foreach ($cc_info as $k => $v) {
                $temp['news_type'] = 1;   //消息类型 1:审批提醒
                //$temp['content'] = $apply_info->approval_type_name;//.'申请';   //消息内容
                $temp['content'] = $applyModel->name;   //消息内容
                $temp['user_type'] = 3;   //消息的用户接收类型(1:申请人;2:审批人;3:抄送人;4:转批接收人;)
                $temp['to_user_id'] = $v['user_id'];    //消息接收人
                $temp['pc'] = $apply_info->pc;
                $temp['wechat'] = $apply_info->wechat;
                $temp['email'] = $apply_info->email;
                $temp['status'] = 1;  //状态 (1:未读;2:已读;3:已忽略;4:已删除;)
                $temp['start_user_id'] = $apply_info->created_by;
                $temp['apply_id'] = $apply_id;
                $temp['start_department_id'] = $apply_info->hrm_department_id;
                $temp['start_department'] = $apply_info->hrm_department;
                $temp['show_time'] = Common::num2DateTime(time());
                $temp['start_at'] = $apply_info->created_at;
                $temp['created_at'] = Common::num2DateTime(time());
                $temp['created_by'] = 0;  //创建人(0:系统;)
                $temp['op_user_ids'] = $op_user_ids;
                $temp['op_user_names'] = $op_user_names;
                $temp['op_type'] = 1;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回;
                
                $insert_data[] = $temp;
            }
        }
        
        return OANewsNotice::insert($insert_data);
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-8-6 上午11:08
     * @Description: 根据申请编号添加给审批人的消息提醒     【 给审批人发送提醒通知 】
     * @param int $apply_id 申请id
     * @param int $apply_status 申请状态    (5:已撤回;) 【申请人撤回时 必传】
     * @param int $op_user_id 最后操作人id [整体流程同意/不同意/撤回 时的用户id]
     * @return boolean
     */
    public static function addNewsToApprover($apply_id, $apply_status = '', $op_user_id = '') {
        $result = FALSE;
        $apply_info = OAApply::where('id', $apply_id)->first();
        if(!$apply_info){
            return $result;
        }
        $applyModel = OAApprovalModel::where('id', $apply_info->approval_model_id)->first();
        if (!$applyModel) {
            return $result;
        }
        $approver_info = OAApplyFlow::getApproverByApplyId($apply_id, 1, $apply_info->level);   //获取下级审批人
        if(empty($approver_info)){
            return $result;
        }
        
        $insert_data = [];  //待插入数据
        if($apply_status != ''){
            //审批完成 ……   申请状态  5:已撤回;
            if($apply_status == 5){
                $op_type = 4;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
            } else {
                return $result;
            }
            
            //获取最后操作人信息 start ……
            $op_user_ids = '';
            $op_user_names = '';
            if ($op_user_id != ''){
                $op_user_info = OAUser::getUserById($op_user_id);
                if($op_user_info){
                    $op_user_ids = $op_user_info->id;
                    $op_user_names = $op_user_info->real_name;
                }
            }
            //获取最后操作人信息 end ……
            
            foreach ($approver_info as $k => $v) {
                $temp['news_type'] = 1;   //消息类型 1:审批提醒
                //$temp['content'] = $apply_info->approval_type_name;//.'申请';   //消息内容
                $temp['content'] = $applyModel->name;   //消息内容
                $temp['user_type'] = 2;   //消息的用户接收类型(1:申请人;2:审批人;3:抄送人;4:转批接收人;)
                $temp['to_user_id'] = $v['user_id'];    //消息接收人
                $temp['pc'] = $apply_info->pc;
                $temp['wechat'] = $apply_info->wechat;
                $temp['email'] = $apply_info->email;
                $temp['status'] = 1;  //状态 (1:未读;2:已读;3:已忽略;4:已删除;)
                $temp['start_user_id'] = $apply_info->created_by;
                $temp['apply_id'] = $apply_id;
                $temp['start_department_id'] = $apply_info->hrm_department_id;
                $temp['start_department'] = $apply_info->hrm_department;
                $temp['show_time'] = Common::num2DateTime(time());
                $temp['start_at'] = $apply_info->created_at;
                $temp['created_at'] = Common::num2DateTime(time());
                $temp['created_by'] = 0;  //创建人(0:系统;)
                $temp['op_user_ids'] = $op_user_ids;
                $temp['op_user_names'] = $op_user_names;
                $temp['op_type'] = $op_type;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回;
                
                $insert_data[] = $temp;
            }
        } else {
            //审批中
            $op_user_ids = '';
            $op_user_names = '';
            foreach ($approver_info as $k => $v) {
                $op_user_ids .= $v['user_id'].',';
                $op_user_names .= $v['real_name'].',';
            }
            $op_user_ids = rtrim($op_user_ids, ',');
            $op_user_names = rtrim($op_user_names, ','); 
            
            foreach ($approver_info as $k => $v) {
                $temp['news_type'] = 1;   //消息类型 1:审批提醒
                //$temp['content'] = $apply_info->approval_type_name.'申请';   //消息内容
                $temp['content'] = $applyModel->name;//.'申请';   //消息内容
                $temp['user_type'] = 2;   //消息的用户接收类型(1:申请人;2:审批人;3:抄送人;4:转批接收人;)
                $temp['to_user_id'] = $v['user_id'];    //消息接收人
                $temp['pc'] = $apply_info->pc;
                $temp['wechat'] = $apply_info->wechat;
                $temp['email'] = $apply_info->email;
                $temp['status'] = 1;  //状态 (1:未读;2:已读;3:已忽略;4:已删除;)
                $temp['start_user_id'] = $apply_info->created_by;
                $temp['apply_id'] = $apply_id;
                $temp['start_department_id'] = $apply_info->hrm_department_id;
                $temp['start_department'] = $apply_info->hrm_department;
                $temp['show_time'] = Common::num2DateTime(time());
                $temp['start_at'] = $apply_info->created_at;
                $temp['created_at'] = Common::num2DateTime(time());
                $temp['created_by'] = 0;  //创建人(0:系统;)
                $temp['op_user_ids'] = $op_user_ids;
                $temp['op_user_names'] = $op_user_names;
                $temp['op_type'] = 1;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回;
                
                $insert_data[] = $temp;
            }
        }
        
        return OANewsNotice::insert($insert_data);
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-8-23 上午9:18
     * @Description: 添加给转发审批的接收人的消息提醒     【 给申请的转批人的通知 】
     * @param int $apply_id 申请id
     * @param int $from_user_id     转批发起人
     * @param int $to_user_id       转批接收人
     * @return boolean
     */
    public static function addNewsToTransferApprover($apply_id, $from_user_id, $to_user_id) {
        $result = FALSE;
        $apply_info = OAApply::where('id', $apply_id)->first();
        if(!$apply_info){
            return $result;
        }
        $applyModel = OAApprovalModel::where('id', $apply_info->approval_model_id)->first();
        if (!$applyModel) {
            return $result;
        }
        $news = new OANewsNotice();
        $news->news_type = 1;   //消息类型 1:审批提醒
        $news->content = $applyModel->name;   //消息内容
        //$news->content = '转发审批';   //消息内容
        $news->user_type = 4;   //消息的用户接收类型(1:申请人;2:审批人;3:抄送人;4:转批接收人;)
        $news->to_user_id = $to_user_id;    //消息接收人
        $news->pc = $apply_info->pc;
        $news->wechat = $apply_info->wechat;
        $news->email = $apply_info->email;
        $news->status = 1;  //状态 (1:未读;2:已读;3:已忽略;4:已删除;)
        $news->start_user_id = $apply_info->created_by;
        $news->apply_id = $apply_id;
        $news->start_department_id = $apply_info->hrm_department_id;
        $news->start_department = $apply_info->hrm_department;
        $news->show_time = Common::num2DateTime(time());
        $news->start_at = $apply_info->created_at;
        $news->created_at = Common::num2DateTime(time());
        $news->created_by = 0;  //创建人(0:系统;)
        $op_user_ids = '';
        $op_user_names = '';
        $from_user_info = OAUser::getUserById($from_user_id);
        if($from_user_info){
            $op_user_ids = $from_user_info->id;
            $op_user_names = $from_user_info->real_name;
        }
        $news->op_user_ids = $op_user_ids;          //转发审批人id
        $news->op_user_names = $op_user_names;      //转发审批人姓名
        $news->op_type = 1;    //操作状态 1:待审批;2:审批驳回;3:审批完成;4:已撤回
        
        return $news->save();
    }



}